<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="组织名称" prop="orgName">
				<el-input v-model="form.orgName" placeholder="组织名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="电话" prop="orgTel">
				<el-input v-model="form.orgTel" placeholder="电话" clearable></el-input>
			</el-form-item>
			<el-form-item label="传真" prop="orgFax">
				<el-input v-model="form.orgFax" placeholder="传真" clearable></el-input>
			</el-form-item>
			<el-form-item label="邮件" prop="orgEmail">
				<el-input v-model="form.orgEmail" placeholder="邮件" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="说明">
				<el-input type="textarea" :rows="2" maxlength="150" placeholder="说明" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import scCron from '@/components/scCron';

const {verifyEmail} = require("@/utils/verificate");

export default {
	components: {
		scCron
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增组织',
				edit: '编辑组织'
			},
			form: {
				tempOrgId: "",
				sortNo:"",
				orgName: "",
				orgAdd: "",
				orgTel: "",
				orgFax: "",
				orgEmail: "",
				remark: "",
				status: "1"
			},
			rules: {
				sortNo: [
					{required: true, message: '请输入排序', trigger: 'change'}
				],
				orgName: [
					{required: true, message: '请填写组织名称'}
				],
				orgEmail: [
					{validator: verifyEmail, trigger:'blur' }
				],

			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					var res;
					if(this.mode=="add")
					{
						this.isSaveing = true;
						res = await this.$API.system.tempOrg.insert.post(this.form);
						this.isSaveing = false;
					}else if(this.mode=="edit")
					{
						this.isSaveing = true;
						res = await this.$API.system.tempOrg.update.post(this.form);
						this.isSaveing = false;
					}
					if (res.code == 200) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			console.log(data.status);
			this.form.tempOrgId = data.tempOrgId
			this.form.sortNo = data.sortNo
			this.form.orgName = data.orgName
			this.form.orgAdd = data.orgAdd
			this.form.orgTel = data.orgTel
			this.form.orgFax = data.orgFax
			this.form.orgEmail = data.orgEmail
			this.form.remark = data.remark
			this.form.status = data.status
		}
	}
}
</script>

<style>
</style>
